package com.transpeed.payservice.main;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

import com.transpeed.payservice.Utils.XmlFormatter;
import com.transpeed.payservice.log.InitLogger;
import com.transpeed.payservice.log.Log;



/**
 * TODO  用于POST各种数据到各种接口（待修改使用成OKhttp)
 * @author lizhi
 * @date 2017年4月25日
 * 更新日志
 * 2017年4月25日 lizhi 首次创建
 *
 */

public class PostMessage {
	private final static int CONNECT_TIMEOUT = 5000;//限制请求时间 	
	
	public static String AlipaySeverPost(String Message, String NotifyUrl,String LogType) {
		String response = null;// 存POST后的返回值
		try {
			// 创建连接
			InitLogger.Createfolder();

			if (NotifyUrl.indexOf("http://") == -1) // 等于-1表示这个字符串中没有o这个字符
				NotifyUrl = "http://" + NotifyUrl;// 服务器接收地址

			Log.Write("接收地址: " + NotifyUrl, LogType);

			URL url = new URL(NotifyUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setConnectTimeout(CONNECT_TIMEOUT);
			connection.setReadTimeout(CONNECT_TIMEOUT);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			connection.connect();
			// POST请求
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			Log.Write("发送信息为: " + Message, LogType);
			out.writeBytes(Message);
			out.flush();
			out.close();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String lines;
			StringBuffer info = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				info.append(lines);
			}

			response = info.toString(); // 对返回值做处理
			Log.Write("返回值为" + response, LogType);
			reader.close();
			// 断开连接
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static String PosPost(String Message, String NotifyUrl,String LogType) {
		String response = null;// 存POST后的返回值
		try {
			// 创建连接
			InitLogger.Createfolder();

			if (NotifyUrl.indexOf("http://") == -1) // 等于-1表示这个字符串中没有o这个字符
				NotifyUrl = "http://" + NotifyUrl;// 服务器接收地址

			Log.Write("接收地址: " + NotifyUrl, LogType);
			URL url = new URL(NotifyUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(CONNECT_TIMEOUT);
			connection.setReadTimeout(CONNECT_TIMEOUT);
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			connection.connect();
			// POST请求
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			Log.Write("发送信息为: " + Message, LogType);
			out.writeBytes(Message);
			out.flush();
			out.close();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String lines;
			StringBuffer info = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				info.append(lines);
			}

			response = info.toString(); // 对返回值做处理
			Log.Write("返回值为" + response, LogType);
			reader.close();
			// 断开连接
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return response;
	}

	public static String WeChatPost(String message,String ADD_URL) {
		InitLogger.Createfolder();
		String response = null;
		try {
			
			if (ADD_URL.indexOf("http://") == -1 && ADD_URL.indexOf("https://") == -1 ) // 等于-1表示这个字符串中没有o这个字符
				ADD_URL = "http://" + ADD_URL;// 服务器接收地址
			// 创建连接
			Log.Write("发送地址为：" +ADD_URL, "WeChat");
			URL url = new URL(ADD_URL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(CONNECT_TIMEOUT);
			connection.setReadTimeout(CONNECT_TIMEOUT);
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			connection.connect();
			// POST请求
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			Log.Write(message, "WeChat");

			byte[] bytes = message.getBytes("UTF-8");

			out.write(bytes);
			out.flush();
			out.close();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "gbk");
				sb.append(lines);
			}

			reader.close();
			// 断开连接
			connection.disconnect();
			response = sb.toString();
			if(response!=null)
			Log.Write("收到微信结果：\n" + new XmlFormatter().format(response),"WeChat");

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return response;
	}

//	public static void main(String[] args) {
//		AlipaySeverPost(
//				"Alipay&Success&AliPayTranSpeed1491555267133952096",
//				"http://112.81.110.151:4000/transpeed-PayService/TestWebDaoImpl/Test",
//				"Main");
//	}

}
